package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.app.common.showPageType
import com.ruoyi.back.domain.Function
import com.ruoyi.back.domain.OpenAdvertising
import com.ruoyi.back.domain.SpreadCenter
import com.ruoyi.back.domain.vo.UrlGoal
import com.ruoyi.back.mapper.OpenAdvertisingMapper
import com.ruoyi.back.service.FunctionService
import com.ruoyi.back.service.OpenAdvertisingService
import com.ruoyi.back.service.SpreadCenterService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class OpenAdvertisingServiceImpl : ServiceImpl<OpenAdvertisingMapper, OpenAdvertising>(), OpenAdvertisingService {

    @Autowired
    private lateinit var functionService: FunctionService

    @Autowired
    private lateinit var spreadCenterService: SpreadCenterService

    override fun saveOpenAdvertising(openAdvertising: OpenAdvertising): Data<Boolean> =
        openAdvertising.id?.run {
            Ok("更新成功", updateById(openAdvertising))
        } ?: kotlin.run {
            Ok("保存成功", save(openAdvertising))
        }

    override fun openAdvertisingPage(openAdvertising: OpenAdvertising): Page<OpenAdvertising> =
        TableSupport.buildPageRequest().let { pageDomain ->
            page(
                Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()),
                KtQueryWrapper(OpenAdvertising::class.java)
                    .eq(openAdvertising::advertisingType, openAdvertising.advertisingType)
                    .orderByDesc(OpenAdvertising::createTime)
            )
                .let { page: Page<OpenAdvertising> ->
                    page.apply {
                        records = page.records.takeIf { it.isNotEmpty() }?.run {
                            map {
                                it.apply {
                                    staySecondsDesc = "${it.staySeconds}s"
                                    showPageDesc = it.showPage?.showPageType()
                                }
                            }
                        }
                    }
                }
        }

    override fun removeOpenAdvertising(advertisingIds: String): Data<Boolean> =
        Ok("批量移除成功", removeByIds(advertisingIds.split(",")))

    override fun urlGoalList(): Data<List<UrlGoal>> =
        Ok("获取目标链接成功", listOf(
            //会员中心
            UrlGoal().apply {
                name = "会员中心"
                url = "in:memberCenter"
            },

            //推广中心
            UrlGoal().apply {
                name = "推广中心"
                urlGoal = spreadCenterService.list(
                    KtQueryWrapper(SpreadCenter::class.java)
                        .eq(SpreadCenter::isShow, true)
                )
                    .takeIf { it.isNotEmpty() }
                    ?.run {
                        val urlGoals = map {
                            UrlGoal().apply {
                                name = it.appName
                                it.apkUrl?.run {
                                    url = it.apkUrl
                                }
                                it.threeUrl?.run {
                                    urlStyle = 2
                                    url = it.threeUrl
                                }
                            }
                        }.toMutableList()
                        urlGoals.add(0, UrlGoal().apply {
                            name = "推广中心当前页"
                            url = "in:spreadCenter"
                        })
                        urlGoals
                    }
            }

        ))
}